
#include <BStyle>
#include <BReadWrite>
#include <BEntry>
#include <BFolder>
#include <BLabel>
#include <BListItem>
#include <BHBoxLayout>
#include <BVBoxLayout>
#include <BMessageBox>
#include <BCursor>
#include <BHtmlWidget>
#include <BApplication>

#include "XmlTreeWidget.h"
#include "MainWindow.h"

MainWindow::MainWindow()
{
	_htmlWidget = 0;

	_fileDialog = new BFileDialog(this);
	_checkDialog = new CheckDialog(this);
	_checkDialog->setSize(800, 600);

	_listMenu = new BMenu(this);
	_listMenu->addAction("open");
	_listMenu->addAction("fresh");
	_listMenu->addAction("check");

	_pageMenu = new BMenu(this);
	//_pageMenu->setForm(Form_TextOnly);
	_pageMenu->addAction("open source")->setName("action-source");
	_pageMenu->addAction("show tree")->setName("action-tree");

	_tableMenu = new BMenu(this);
	_tableMenu->addAction("add row")->setName("action-add-row");
	_tableMenu->addAction("insert row")->setName("action-insert-row");
	_tableMenu->addAction("remove row")->setName("action-remove-row");
	_tableMenu->addAction("add column")->setName("action-add-column");
	_tableMenu->addAction("insert column")->setName("action-insert-column");
	_tableMenu->addAction("remove column")->setName("action-remove-column");

	_tabMenu = new BMenu(this);
	_tabMenu->addAction("Close Current Document")->setName("action-close-tab");
	_tabMenu->addAction("Close All Document")->setName("action-close-all-tab");
	_tabMenu->addAction("Close All Except Current")->setName("action-close-all-except-current");

	_openButton = new BButton("open");
	_saveButton = new BButton("save");
	_saveButton->setEnabled(false);
	_saveAllButton = new BButton("save-all");
	_saveAllButton->setEnabled(false);

	BLabel* headLabel = new BLabel("head-path");
	_headPathEdit = new BLineEdit();

	BLabel* htmlLable = new BLabel("html-path");
	_htmlPathEdit = new BLineEdit();

	_readOnlyButton = new BButton("ReadOnly");
	_readOnlyButton->setCheckable(true);

	_menuBar = new BMenuBar();
	_menuBar->setSpacing(3);
	{
		BMenu* headMenu = new BMenu("head");
		headMenu->addAction("h1");
		headMenu->addAction("h2");
		headMenu->addAction("h3");
		headMenu->addAction("h4");
		headMenu->addAction("h5");
		headMenu->addAction("h6");

		BAction* pAction = new BAction("p");
		BAction* aAction = new BAction("a");
		BAction* textAction = new BAction("text");
		BAction* divAction = new BAction("div");
		BAction* tableAction = new BAction("table");
		BAction* imgAction = new BAction("img");

		BMenu* addMenu = new BMenu("Add");
		addMenu->setName("menu-add");
		addMenu->addMenu(headMenu);
		addMenu->addAction(pAction);
		addMenu->addAction(aAction);
		addMenu->addAction(textAction);
		addMenu->addAction(imgAction);
		addMenu->addAction(divAction);
		addMenu->addAction(tableAction);

		BMenu* prependMenu = new BMenu("Prepend");
		prependMenu->setName("menu-prepend");
		prependMenu->addMenu(headMenu);
		prependMenu->addAction(pAction);
		prependMenu->addAction(aAction);
		prependMenu->addAction(textAction);
		prependMenu->addAction(imgAction);
		prependMenu->addAction(divAction);
		prependMenu->addAction(tableAction);

		BMenu* appendMenu = new BMenu("Append");
		appendMenu->setName("menu-append");
		appendMenu->addMenu(headMenu);
		appendMenu->addAction(pAction);
		appendMenu->addAction(aAction);
		appendMenu->addAction(textAction);
		appendMenu->addAction(imgAction);
		appendMenu->addAction(divAction);
		appendMenu->addAction(tableAction);

		_menuBar->addMenu(addMenu);
		_menuBar->addMenu(prependMenu);
		_menuBar->addMenu(appendMenu);

		_pageMenu->addMenu(addMenu);
		_pageMenu->addMenu(prependMenu);
		_pageMenu->addMenu(appendMenu);

		connect(addMenu, Signal_Triggered, &MainWindow::slotMenuAddTrigger);
		connect(prependMenu, Signal_Triggered, &MainWindow::slotMenuPrependTrigger);
		connect(appendMenu, Signal_Triggered, &MainWindow::slotMenuAppendTrigger);
	}

	_removeButton = new BButton("remove");

	BHBoxLayout* toolLayout = new BHBoxLayout();
	toolLayout->addWidget(_openButton);
	toolLayout->addWidget(_saveButton);
	toolLayout->addWidget(_saveAllButton);
	toolLayout->addSpring(100);
	toolLayout->addWidget(_readOnlyButton);
	toolLayout->addSpacer(10);
	toolLayout->addWidget(_menuBar);
	toolLayout->addSpacer(10);
	toolLayout->addWidget(_removeButton);
	toolLayout->addSpring(100);
	toolLayout->addWidget(headLabel);
	toolLayout->addWidget(_headPathEdit, 10);
	toolLayout->addWidget(htmlLable);
	toolLayout->addWidget(_htmlPathEdit, 10);

	BHBoxLayout* splitLayout = new BHBoxLayout();
	splitLayout->setSplitable(true);
	splitLayout->setSpacing(6);
	{
		_headListWidget = new BListWidget();
		_headListWidget->setSelectMode(Select_Single);

		_tabWidget = new BTabWidget();

		_propertyBox = new PropertyBox();
		_propertyBox->setVisible(true);

		splitLayout->addWidget(_headListWidget);
		splitLayout->addWidget(_tabWidget, 4);
		splitLayout->addWidget(_propertyBox);
	}

	BVBoxLayout* layout = new BVBoxLayout(this);
	layout->addLayout(toolLayout, 0);
	layout->addLayout(splitLayout);

	connect(_listMenu, Signal_Triggered, &MainWindow::slotListMenuTrigger);
	connect(_pageMenu, Signal_Triggered, &MainWindow::slotPageMenuTrigger);
	connect(_tableMenu, Signal_Triggered, &MainWindow::slotTableMenuTrigger);
	connect(_tabMenu, Signal_Triggered, &MainWindow::slotTabMenuTrigger);

	connect(_openButton, Signal_Clicked, &MainWindow::slotOpenDocument);
	connect(_saveButton, Signal_Clicked, &MainWindow::slotSaveDocument);
	connect(_saveAllButton, Signal_Clicked, &MainWindow::slotSaveAllDocument);
	connect(_readOnlyButton, Signal_Clicked, &MainWindow::slotReadOnlyClicked);
	connect(_headPathEdit, Signal_DoubleClicked, &MainWindow::slotHeadPathEditDoubleClicked);
	connect(_htmlPathEdit, Signal_DoubleClicked, &MainWindow::slotHtmlPathEditDoubleClicked);
	connect(_headListWidget, Signal_ItemDoubleClicked, &MainWindow::slotHeadItemDoubleClicked);
	connect(_headListWidget, Signal_ItemRightClicked, &MainWindow::slotHeadItemRightClicked);

	connect(_tabWidget, Signal_CurrentChanged, &MainWindow::slotTabCurrentChanged);
	connect(_tabWidget, Signal_TabClosing, &MainWindow::slotTabClosing);
	connect(_tabWidget, Signal_TabClicked, &MainWindow::slotTabClicked);
	connect(_tabWidget, Signal_TabRightClicked, &MainWindow::slotTabRightClicked);

	connect(_removeButton, Signal_Clicked, &MainWindow::slotRemoveButtonClicked);

	_headPathEdit->setText("../../include");
	_htmlPathEdit->setText("../../manual/htmls");
	freshHeads();
	this->openHtmlFile("../../manual/index.html");
}
MainWindow::~MainWindow()
{

}

void MainWindow::slotOpenDocument()
{
	BString fileName = BFileDialog::OpenFile(0, "Open html file", _htmlPathEdit->text(), "*.html");
	if (fileName.size())
	{
		if (!openHtmlFile(fileName))
		{
			BString text("Open html file ");
			text << fileName + " Failed.";
			Button result = BMessageBox::Warning(this, "Question", text);
		}
	}
}
void MainWindow::slotSaveDocument()
{
	BHtmlWidget* htmlWidget = dynamic_cast<BHtmlWidget*>(_tabWidget->currentWidget());
	if (htmlWidget)
	{
		if (!htmlWidget->save(htmlWidget->name()))
		{
			BString text("Save html file ");
			text << htmlWidget->name() << " Failed.";
			BMessageBox::Critical(this, "Critical", text);
		}
		else
		{
			signTitleChanged(htmlWidget, false);
		}
	}
}
void MainWindow::slotSaveAllDocument()
{
	for (int i = 0; i < _tabWidget->tabCount(); i++)
	{
		BWidget* tab = _tabWidget->tab(i);
		BHtmlWidget* htmlWidget = dynamic_cast<BHtmlWidget*>(tab);
		if (!htmlWidget || htmlWidget->title().first() != '*')
			continue;
		if (htmlWidget->save(htmlWidget->name()))
			signTitleChanged(htmlWidget, false);
	}
}

void MainWindow::slotHeadPathEditDoubleClicked()
{
	if (_fileDialog->execute())
	{
		const BString& fileName = _fileDialog->entryName();
		_headPathEdit->setText(fileName);
		freshHeads();
	}
}
void MainWindow::slotHtmlPathEditDoubleClicked()
{
	if (_fileDialog->execute())
	{
		const BString& fileName = _fileDialog->entryName();
		_htmlPathEdit->setText(fileName);
		freshHeads();
	}
}

void MainWindow::slotHeadItemRightClicked(BObject* object, const BValue& value)
{
	BListItem* item = value;
	if (!item)
		return;
	const BPoint& point = BCursor::GetPos();
	BPoint pos = point - this->pos();
	_listMenu->setPos(pos);
	_listMenu->popup();
}
void MainWindow::slotHeadItemDoubleClicked(BObject* object, const BValue& value)
{
	BListItem* item = value;
	if (!item)
		return;
	const HeadInfo* info = item->asset();
	if(info && info->htmlEntry)
	{
		const BString& htmlName = info->htmlEntry->path();
		if (htmlName.empty())
		{
			checkHeadFile(item);
		}
		else
		{
			openHtmlFile(htmlName);
		}
	}
}

void MainWindow::slotTabCurrentChanged(BObject* object, const BValue& value)
{
	_htmlWidget = value;
	_propertyBox->attachHtmlWidget(_htmlWidget);
	if (_htmlWidget)
	{
		_readOnlyButton->setChecked(_htmlWidget->readOnly());
	}
	bool enabled = _htmlWidget != 0;
	_menuBar->setEnabled(enabled);
	_readOnlyButton->setEnabled(enabled);
	_removeButton->setEnabled(enabled);
}
void MainWindow::slotTabClicked(BObject* object, BValue& value)
{
	BWidget* widget = value;
	if (widget)
	{
		const BString& title = widget->title();
		BListItem* item = _headListWidget->item(title);
		if (item)
		{
			item->setSelected(true);
			_headListWidget->scrollToItem(item, Align_VCenter);
		}
		else
		{
			_headListWidget->clearSelection();
		}
	}
}
void MainWindow::slotTabRightClicked(BObject* object, BValue& value)
{
	BWidget* widget = value;
	const BPoint& point = BCursor::GetPos();
	BPoint pos = point - this->pos();
	_tabMenu->setPos(pos);
	_tabMenu->popup();
}

void MainWindow::slotTabClosing(BObject* object, BValue& value)
{
	BHtmlWidget* htmlWidget = value;
	if (htmlWidget && htmlWidget->title().first() == '*')
	{
		Button btn = BMessageBox::Question(this, "Close Html", "Do you want close the html document ?");
		if (btn == Button_No)
			value = 0;
	}
}
void MainWindow::slotReadOnlyClicked(BObject* object, const BValue& value)
{
	bool checked = _readOnlyButton->checked();
	_htmlWidget->setReadOnly(checked);
}
void MainWindow::slotRemoveButtonClicked(BObject* object, const BValue& value)
{
	BHtmlItem* item = _htmlWidget->selectedItem();
	_htmlWidget->removeItem(item);
}

void MainWindow::slotListMenuTrigger(BObject* object, const BValue& value)
{
	BAction* action = value;
	if (!action)
		return;
	const BString& text = action->text();
	if (text == "open")
	{
		BListItem* item = _headListWidget->selectedItem();
		if (!item)
			return;
		const BString& fileName = item->asset();
		if (!BFile::Exist(fileName))
		{
			BString text = _headPathEdit->text() + item->text() + ".html is not exist.\nPlease create docment first.";
			Button result = BMessageBox::Information(this, "Question", text);
			return;
		}
		if (!openHtmlFile(fileName))
		{
			BString text("Open html file ");
			text << _htmlPathEdit->text() <<  item->text() + ".html Failed.";
			Button result = BMessageBox::Warning(this, "Question", text);
		}
		return;
	}
	if (text == "fresh")
	{
		return;
	}
	if (text == "check")
	{
		BListItem* item = _headListWidget->selectedItem();
		checkHeadFile(item);
		return;
	}
}
void MainWindow::slotPageMenuTrigger(BObject* object, const BValue& value)
{
	BAction* action = value;
	if (!action)
		return;
	if (action->name() == "action-source")
	{
		BHtmlWidget* html = dynamic_cast<BHtmlWidget*>(_tabWidget->currentWidget());
		if (!html)
			return;
		BTextEdit* textEdit = new BTextEdit();
		textEdit->setTitle(html->title() + ".html");
		textEdit->setName(html->name());
		textEdit->setTitleIcon(_textIcon);
		textEdit->setReadOnly(true);
		textEdit->load(html->name());

		_tabWidget->addTab(textEdit, true);
		_tabWidget->setCurrentWidget(textEdit);
		return;
	}
	if (action->name() == "action-tree")
	{
		BHtmlWidget* html = dynamic_cast<BHtmlWidget*>(_tabWidget->currentWidget());
		if (!html)
			return;
		BXmlNode* body = html->body();
		BXmlNode* root = body->root();

		XmlTreeWidget* xmlTree = new XmlTreeWidget();
		xmlTree->setTitle(html->title() + ".html");
		xmlTree->setName(html->name());
		xmlTree->setTitleIcon(_treeIcon);
		xmlTree->reset(root);

		_tabWidget->addTab(xmlTree, true);
		_tabWidget->setCurrentWidget(xmlTree);
	}
}
void MainWindow::slotTableMenuTrigger(BObject* object, const BValue& value)
{
	BAction* action = value;
	if (!action)
		return;
	BHtmlWidget* htmlWidget = dynamic_cast<BHtmlWidget*>(_tabWidget->currentWidget());
	if (!htmlWidget)
		return;
	BHtmlItem* item = htmlWidget->selectedItem();
	if (!item)
		return;
	if (item->name() == "td")
	{
		BHtmlItem* table = item->parent("table");
		int row = 0;
		int column = 0;
		int columnCount = 0;
		for (int r = 0; r < table->childCount(); r++)
		{
			BHtmlItem* rowItem = table->child(r);
			if (columnCount < rowItem->childCount())
				columnCount = rowItem->childCount();
			for (int c = 0; c < rowItem->childCount(); c++)
			{
				BHtmlItem* tdItem = rowItem->child(c);
				if (tdItem == item)
				{
					row = r;
					column = c;
					break;
				}
			}
		}
		if (action->name() == "action-add-column")
		{
			for (int r = 0; r < table->childCount(); r++)
			{
				BHtmlItem* rowItem = table->child(r);
				rowItem->addChild(new BHtmlItem("td"));
			}
			return;
		}
		if (action->name() == "action-add-row")
		{
			BHtmlItem* rowItem = new BHtmlItem("tr");
			rowItem->setFormat("\n<");
			for (int c = 0; c < columnCount; c++)
				rowItem->addChild(new BHtmlItem("td"));
			table->addChild(rowItem);
			return;
		}
	}
	if (item->name() == "table")
	{
		int columnCount = 0;
		for (int r = 0; r < item->childCount(); r++)
		{
			BHtmlItem* rowItem = item->child(r);
			if (columnCount < rowItem->childCount())
				columnCount = rowItem->childCount();
		}
	
		if (action->name() == "action-add-row")
		{
			BHtmlItem* rowItem = new BHtmlItem("tr");
			rowItem->setFormat("\n<");
			for (int c = 0; c < columnCount; c++)
				rowItem->addChild(new BHtmlItem("td"));
			item->addChild(rowItem);
			return;
		}
		if (action->name() == "action-add-column")
		{
			for (int i = 0; i < item->childCount(); i++)
			{
				BHtmlItem* rowItem = item->child(i);
				rowItem->addChild(new BHtmlItem("td"));
			}
		}
		return;
	}
}
void MainWindow::slotTabMenuTrigger(BObject* object, const BValue& value)
{
	("action-close-all-except-current");
	BAction* action = value;
	if (action->name() == "action-close-tab")
	{
		BWidget* widget = _tabWidget->currentWidget();
		_tabWidget->removeTab(widget);
		return;
	}
	if (action->name() == "action-close-all-tab")
	{
		_tabWidget->clearTabs();
	}
}

void MainWindow::slotHtmlItemClicked(BObject* object, const BValue& value)
{
	if (!_htmlWidget->readOnly())
		return;
	BHtmlItem* item = value;
	if (item && item->name() == 'a')
	{
		const BString& name = _htmlWidget->name();
		const BString& href = item->attrib("href");
		if (!href.beginWith('#'))
		{
			BString currentPath = BApplication::CurrentPath();
			BString fileName = name.path();
			fileName << '/' << href;
			openHtmlFile(fileName);
		}
	}
}
void MainWindow::slotHtmlItemRightClicked(BObject* object, const BValue& value)
{
	BHtmlWidget* htmlWidget = dynamic_cast<BHtmlWidget*>(object);
	BHtmlItem* item = value;
	const BPoint& point = BCursor::GetPos();
	BPoint pos = point - this->pos();
	_tableMenu->action("add row")->setEnabled(false);
	_tableMenu->action("insert row")->setEnabled(false);
	_tableMenu->action("remove row")->setEnabled(false);
	_tableMenu->action("add column")->setEnabled(false);
	_tableMenu->action("insert column")->setEnabled(false);
	_tableMenu->action("remove column")->setEnabled(false);
	if (item && item->name() == "table")
	{
		_tableMenu->action("add row")->setEnabled(true);
		_tableMenu->action("add column")->setEnabled(true);
		_tableMenu->setPos(pos);
		_tableMenu->popup();
	}
	else if (item && item->name() == "tr")
	{
		_tableMenu->action("add row")->setEnabled(true);
		_tableMenu->action("insert row")->setEnabled(true);
		_tableMenu->action("remove row")->setEnabled(true);
		_tableMenu->setPos(pos);
		_tableMenu->popup();
	}
	else if (item && item->name() == "td")
	{
		_tableMenu->action("add row")->setEnabled(true);
		_tableMenu->action("insert row")->setEnabled(true);
		_tableMenu->action("remove row")->setEnabled(true);
		_tableMenu->action("add column")->setEnabled(true);
		_tableMenu->action("insert column")->setEnabled(true);
		_tableMenu->action("remove column")->setEnabled(true);
		_tableMenu->setPos(pos);
		_tableMenu->popup();
	}
	else
	{
		_pageMenu->setPos(pos);
		_pageMenu->popup();
	}
}
void MainWindow::slotHtmlItemChanged(BObject* object, const BValue& value)
{
	BHtmlWidget* htmlWidget = dynamic_cast<BHtmlWidget*>(object);
	signTitleChanged(htmlWidget, true);
}

void MainWindow::slotMenuAddTrigger(BObject* object, const BValue& value)
{
	BHtmlItem* htmlItem = _htmlWidget->selectedItem();
	if (!htmlItem)
		return;

	BMenu* menu = dynamic_cast<BMenu*>(object);
	if (!menu)
		return;

	BAction* action = value;
	if (!action)
		return;

	BHtmlItemHolder item = new BHtmlItem();
	const BString& text = action->text();
	if (text != "text")
		item->setName(action->text());
	if (text == "p" || text == "div" || (text.size() == 2 && text[0] == 'h'))
		item->setFormat("\n<");

	htmlItem->addChild(item);
	item->setSelected(true);
	_htmlWidget->setFocused(true);

}
void MainWindow::slotMenuPrependTrigger(BObject* object, const BValue& value)
{
	BHtmlItem* htmlItem = _htmlWidget->selectedItem();
	if (!htmlItem)
		return;

	BMenu* menu = dynamic_cast<BMenu*>(object);
	if (!menu)
		return;

	BAction* action = value;
	if (!action)
		return;

	BHtmlItemHolder item = new BHtmlItem();
	const BString& text = action->text();
	if (text != "text")
		item->setName(action->text());
	if (text == "p" || text == "div" || (text.size() == 2 && text[0] == 'h'))
		item->setFormat("\n<");

	BHtmlItem* parentItem = htmlItem->parent();
	int pos = htmlItem->index();
	parentItem->insertChild(pos, item);
	item->setSelected(true);
	_htmlWidget->setFocused(true);
}
void MainWindow::slotMenuAppendTrigger(BObject* object, const BValue& value)
{
	BHtmlItem* htmlItem = _htmlWidget->selectedItem();
	if (!htmlItem)
		return;

	BMenu* menu = dynamic_cast<BMenu*>(object);
	if (!menu)
		return;

	BAction* action = value;
	if (!action)
		return;

	BHtmlItemHolder item = new BHtmlItem();
	const BString& text = action->text();
	if (text != "text")
		item->setName(action->text());
	if (text == "p" || text == "div" || (text.size() == 2 && text[0] == 'h'))
		item->setFormat("\n<");

	BHtmlItem* parentItem = htmlItem->parent();
	int index = htmlItem->index();
	parentItem->insertChild(index + 1, item);
	item->setSelected(true);
	_htmlWidget->setFocused(true);
}

void MainWindow::freshHeads()
{
	_includeFolder.setPath(_headPathEdit->text());
	if (_includeFolder.open() == false)
		return;

	_documentFolder.setPath(_htmlPathEdit->text());
	if (_documentFolder.open() == false)
		return;

	_headListWidget->clearItems();
	for (int i = 0; i < _includeFolder.entryCount(); i++)
	{
		BEntry* entry = _includeFolder.entry(i);
		if (entry->attrib() & Attrib_File)
		{
			HeadInfo* info = new HeadInfo();
			info->headEntry = entry;
			BListItem* item = _headListWidget->addItem(entry->name());
			BString htmlEntryName = entry->name() + ".html";
			info->htmlEntry = _documentFolder.entry(htmlEntryName);
			item->setAsset(info);
		}
	}
}
bool MainWindow::checkHeadFile(BListItem* item)
{
	BString headFileName = _headPathEdit->text() + '/' + item->text();
	BString htmlFileName = _htmlPathEdit->text() + '/' + item->text() + ".html";
	_checkDialog->openHeadFile(headFileName);
	_checkDialog->loadHtmlFile(htmlFileName);
	BString title = "Check Diff - ";
	title << item->text();
	_checkDialog->setTitle(title);
	if (_checkDialog->execute())
	{
		item->setAsset(htmlFileName);
		return true;
	}
	return false;
}
bool MainWindow::openHtmlFile(const BString& fileName)
{
	BString title = fileName.name();
	title -= ".html";

	if (_tabWidget->setCurrentWidget(title))
	{
		return true;
	}

	BHolder<BHtmlWidget> htmlWidget = new BHtmlWidget();
	if (!htmlWidget->load(fileName))
		return false;

	BHtmlItem* body = htmlWidget->body();

	htmlWidget->setName(fileName);
	htmlWidget->setTitle(title);
	htmlWidget->setTitleIcon(_htmlIcon);

	_tabWidget->addTab(htmlWidget, true);
	_tabWidget->setCurrentWidget(htmlWidget);
	connect(htmlWidget, Signal_ItemClicked, &MainWindow::slotHtmlItemClicked);
	connect(htmlWidget, Signal_ItemRightClicked, &MainWindow::slotHtmlItemRightClicked);
	connect(htmlWidget, Signal_ItemChanged, &MainWindow::slotHtmlItemChanged);
	return true;
}
void MainWindow::signTitleChanged(BWidget* widget, bool changed)
{
	const BString& title = widget->title();
	if (changed)
	{
		if (title.first() != '*')
		{
			BString newTitle("*");
			newTitle << title;
			widget->setTitle(newTitle);
		}
	}
	else
	{
		if (title.first() == '*')
		{
			BString newTitle = title;
			newTitle.remove('*');
			widget->setTitle(newTitle);
		}
	}
}
void MainWindow::freshHeadItemIcons(BListItem* item)
{
	if(item)
	{
		const HeadInfo* info = item->asset();
		if(info->htmlEntry)
		{
			if(info->htmlEntry->time() > info->headEntry->time())
				item->setImage(_normalImage);
			else
				item->setImage(_dirtyImage);
		}
		else
		{
			item->setImage(_emptyImage);
		}
	}
}

void MainWindow::updateEvent(const BEvent& event)
{
	if (!_saveButton->enabled() && _tabWidget->tabCount())
	{
		BWidget* widget = _tabWidget->currentWidget();
		if (widget->title().contain('*'))
			_saveButton->setEnabled(true);
	}
	if (!_saveAllButton->enabled() && _saveButton->enabled())
	{
		_saveAllButton->setEnabled(true);
	}
}

void MainWindow::styleEvent(const BEvent& event)
{
	const BStyle* style = event.value();

	_removeButton->setIcon(style->icon("button-remove"));
	_htmlIcon = style->icon("html-icon");
	_textIcon = style->icon("text-edit-icon");
	_treeIcon = style->icon("xml-tree-icon");
	_normalImage = style->image("normal");
	_dirtyImage = style->image("dirty");
	_emptyImage = style->image("empty");
	for (int i = 0; i < _headListWidget->itemCount(); i++)
	{
		freshHeadItemIcons(_headListWidget->item(i));
	}

	for (int i = 0; i < _tabWidget->tabCount(); i++)
	{
		BWidget* widget = _tabWidget->tab(i);
		if (dynamic_cast<BHtmlWidget*>(widget))
		{
			widget->setTitleIcon(_htmlIcon);
			continue;
		}
		if (dynamic_cast<BTextEdit*>(widget))
		{
			widget->setTitleIcon(_textIcon);
			continue;
		}
		if (dynamic_cast<BTreeWidget*>(widget))
		{
			widget->setTitleIcon(_treeIcon);
			continue;
		}
	}

	for (int i = 0; i < _pageMenu->count(); i++)
	{
		BAction* action = _pageMenu->action(i);
		if (action)
		{
			action->setIcon(style->icon(action->name()));
		}
		BMenu* menu = _pageMenu->menu(i);
		if (menu)
		{
			menu->setTitleIcon(style->icon(menu->name()));
		}
	}

	for (int i = 0; i < _listMenu->count(); i++)
	{
		BAction* action = _listMenu->action(i);
		if (action)
		{
			action->setIcon(style->icon(action->text()));
		}
	}
}
